package Web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.mortbay.util.ajax.Continuation;
import org.mortbay.util.ajax.ContinuationSupport;
import Command.MainCommand;

public class ForeverFrameServlet extends HttpServlet {
	private MainCommand mainCommand;
	public ForeverFrameServlet(MainCommand mainCommand){
		this.mainCommand=mainCommand;
	}

	
   public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      if (req.getRequestURI().endsWith("/time")) {
         Continuation cc = ContinuationSupport.getContinuation(req, null);
         resp.setContentType("text/html");

         while (true) {
            cc.suspend(1000); // suspend the response
            String script = "<script>\r\n" +
                            "  parent.printTime(\" " + new Date() + "\");\r\n" +
                            "</script>";
            resp.getWriter().println(script);
            resp.getWriter().flush();
         }
      }else if (req.getRequestURI().endsWith("/time1")) {
          resp.setContentType("text/html");
          String script = "<script>\r\n" +
                             "  parent.printTime(\"stop\");\r\n" +
                             "</script>";
             resp.getWriter().println(script);
       }else if (req.getRequestURI().endsWith("/start_listener")) {
          resp.setContentType("text/html");
             String script = "<script>\r\r\r\n\n\n" +
             "  parent.printListener(\"start listening\");\r\n" +
             "</script>";
             String script2 = "<script>\r\r\r\n\n\n" +
             "  parent.printListener(\"the system alradey listening\");\r\n" +
             "</script>";
             if (mainCommand.startObserver())
            	 resp.getWriter().println(script);
             else
            	 resp.getWriter().println(script2);
       }else if (req.getRequestURI().endsWith("/end_listener")) {
           resp.setContentType("text/html");
              String script = "<script>\r\r\r\n\n\n" +
              "  parent.printListener(\"stop listening\");\r\n" +
              "</script>";
              String script2 = "<script>\r\r\r\n\n\n" +
              "  parent.printListener(\"the system isnt listening\");\r\n" +
              "</script>";
              if (mainCommand.stopObserver())
             	 resp.getWriter().println(script);
              else
             	 resp.getWriter().println(script2);
        }else if (req.getRequestURI().endsWith("/start_init")) {
            Continuation cc = ContinuationSupport.getContinuation(req, null);
            resp.setContentType("text/html");
            mainCommand.reStartInit();
            int j,i=0;
            while (i<98) {
            	i=mainCommand.returnPrecentInit();
                cc.suspend(800); // suspend the response
                String txt=" ";
                for (j=0;j<i;j++)
                	txt=txt+"||";
                String script = "<script>\r\n" +
                "  parent.printInit(\" " + txt +"  " + i +"%"+"\");\r\n" +
                "</script>";
                resp.getWriter().println(script);
                resp.getWriter().flush();
        	}
            i=100;
            String txt=" ";
            for (j=0;j<i;j++)
            	txt=txt+"||";
            String script = "<script>\r\n" +
            "  parent.printInit(\" " + txt +"  " + i +"%"+"\");\r\n" +
            "</script>";
            resp.getWriter().println(script);
            
         }else if (req.getRequestURI().endsWith("/stop_init")) {
             resp.setContentType("text/html");
             mainCommand.stopInit();
             String script = "<script>\r\n" +
             "  parent.printInit(\" "+"\");\r\n" +
             "</script>";
             resp.getWriter().println(script);
          }else if (req.getRequestURI().endsWith("/pause_init")) {
             resp.setContentType("text/html");
             int i=mainCommand.returnPrecentInit();
             mainCommand.pauseInit();
             int j;
             String txt=" ";
             for (j=0;j<i;j++)
             	txt=txt+"||";
             String script = "<script>\r\n" +
             "  parent.printInit(\" " + txt +"  " + i +"%"+"\");\r\n" +
             "</script>";
             resp.getWriter().println(script);
          }else if (req.getRequestURI().endsWith("/search")) {
        	  

           }
   }
}
